use carpet::base::error::{BadRequest, ServerError};
use glass::Reflect;
use thiserror::Error;

use crate::api::prelude::*;
use validator::impls::*;

#[derive(Debug, Reflect, Serialize, Deserialize)]
pub enum Sex {
    Woman,
    Man,
}

/// 请求参数
#[derive(Debug, ApiModel, Validator, Serialize, Deserialize)]
pub struct RequestData {
    /// id
    #[name("id")]
    #[validate(range(23, 234))]
    pub id: i32,

    #[validate(enumer(Sex))]
    pub sex: i32,
}

/// 响应数据
#[derive(Debug, ApiModel, Validator, Serialize, Deserialize)]
pub struct ResponseData {
    /// 消息提示
    #[name("消息")]
    pub message: String,

    #[validate(range(2, 23))]
    pub sex: i32,
}

#[derive(Debug, ApiError, Error)]
pub enum Error {
    #[error(transparent)]
    ServerError(#[from] ServerError),

    #[error(transparent)]
    BadRequest(#[from] BadRequest),
}

/// aaa
#[handler(method = POST, path = "/api/user/{id}")]
async fn _handle(
    self,
    path_data: Query<RequestData>,
    json_data: Json<RequestData>,
) -> Result<ResponseData, Error>;

/// bbb
#[handler(method = POST, path = "/api/user/pc/{id}")]
async fn pc(
    self,
    path_data: Path<RequestData>,
    json_data: Json<RequestData>,
) -> Result<ResponseData, Error>;

/// ccc
#[handler(method = POST)]
async fn mobile(self, json_data: Json<RequestData>) -> Result<ResponseData, Error>;
